package com.yinhe.music.yhmusic.singer.list;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.SingerListAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceViewPagerFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.search.ISearchFragment;
import com.yinhe.music.yhmusic.singer.info.SingerInfoActivity;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;


/**
 * 歌手列表
 *
 * @author hasee
 * @date 2017/2/25
 */

public class SingerListFragment extends BaseServiceViewPagerFragment implements
        ISearchFragment, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener,
        ISingerListContract.ISingerListView {
    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView mRecyclerView;

    private SingerListAdapter mAdapter;
    private String mKey;
    private int mType;
    private int pageNum;
    private int mPageSize = 15;
    private int mPage = 1;

    SingerListPresenter mPresenter;

    public static SingerListFragment newInstance(int singerType, String key) {
        SingerListFragment fragment = new SingerListFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("singer_type", singerType);
        bundle.putString("key", key);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        RxBus.get().unregister(this);
        super.onDestroyView();
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SingerListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        if (getArguments() != null) {
            mKey = getArguments().getString("key");
            mType = getArguments().getInt("singer_type");
        }

        initRecyclerView();
        initAdapter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.music_hall_recycler_layout;
    }

    @Override
    protected void onFragmentFirstVisible() {
        mPage = 1;
        reqSingerList();
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);

        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, getResources().getDimensionPixelOffset(R.dimen.dp_55), false);
        mRecyclerView.addItemDecoration(itemDecoration);

        if (mType == IConstants.SEARCH_SINGER) {
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                        RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
                    }
                }
            });
        }
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {
        if (mType == IConstants.SEARCH_SINGER) {
            mAdapter = new SingerListAdapter(null, mKey);
        } else {
            mAdapter = new SingerListAdapter(null);
        }

        mAdapter.setOnItemClickListener(this);

        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.setPreLoadNumber(5);

        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 请求歌手列表
     */
    private void reqSingerList() {
        LogUtils.d(mType);
        switch (mType) {
            case IConstants.FOLLOW_LIST:
                mPresenter.getCollectSingerList(mPage, mPageSize);
                break;
            case IConstants.SEARCH_SINGER:
                mPresenter.getSearchList(mPage, mPageSize, mKey);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mType == IConstants.SEARCH_LIST) {
            UmengEventUtils.clickSearch(getActivity(), UmengEventUtils.SINGER);
        }
        SingerList singerList = (SingerList) adapter.getData().get(position);
        Intent intent = new Intent(getActivity(), SingerInfoActivity.class);
        intent.putExtra("singerId", singerList.getSingerId());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        if (mPage <= pageNum) {
            reqSingerList();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Singer.COLLECT_SINGER)
    )
    public void onRxbusUpdateSingerList(String id) {
        mPage = 1;
        reqSingerList();
    }

    @Override
    public void updateDataForKey(String key) {
        if (key.equals(mKey)) {
            return;
        }
        mKey = key;
        if (mAdapter != null) {
            mAdapter.setSearchKey(key);
            mPage = 1;
            reqSingerList();
        }
    }

    @Override
    public void setSingerListUI(SingerList info) {
        mRecyclerView.stopScroll();
        List<SingerList> list = info.getSingerList();

        if (list == null) {
            return;
        }

        this.pageNum = info.getPageNum();
        int size = list.size();

        if (mPage == 1) {
            mAdapter.setEmptyView(getEmptyView(mRecyclerView));
            mAdapter.setNewData(list);
        } else {
            if (size > 0) {
                mAdapter.addData(list);
            }
        }

        if (mPage >= pageNum) {
            mAdapter.loadMoreEnd();
        } else {
            mPage++;
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setLoadMoreFailUI() {
        mAdapter.loadMoreFail();
    }
}
